package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class WalkActionListener implements ActionListener{
	
	private WalkHandler wh;
	private boolean inv;
	
	public WalkActionListener(WalkHandler wh){
		this.wh = wh;
	}

	public void setInverse(boolean b){
		this.inv = b;
	}
	
	//Lopen
	public void actionPerformed(ActionEvent arg0) {
		int mx = wh.getMX();
		int my = wh.getMY();
		int px = wh.getPX();
		int py = wh.getPY();
		int speed = wh.getPlayerSpeed();
		
		if(inv){
			speed = -speed/2;
		}
		
		//Kant lopen is gelijk de stand van de muis de manier van lopen hangt af van key
		//momenteel enkel vooruit dus enkel de x springt vooruit behalve wnr de xen gelijk zijn
		
		if(Math.abs(mx-px) > 4 || Math.abs(my-py) > 4){
			if(my == py){
				if(px < mx)
					px += speed;
				else if(px > mx)
					px -= speed;
			}else{
				double party = ((double)(my-py))/((double)Math.sqrt(Math.pow((mx-px),2)+Math.pow((my-py), 2)));
				double partx = ((double)(mx-px))/((double)Math.sqrt(Math.pow((mx-px),2)+Math.pow((my-py), 2)));
				py = (int)(py + party*speed);
				px = (int)(px + partx*speed);
			}
			//If !collision
			//nieuwe playerpositie is px,py... controleren met getTile van Map (8 richtingen)
				wh.moveX(px);
				wh.moveY(py);
			//Endif
				
			wh.repaint();
		}
	}
}
